package com.module.prms.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;
import base.util.StringUtil;
import base.util.Tools;

import com.module.prms.vo.PrmsUser;
import com.module.prms.vo.PrmsUserOrg;



public class UserDao extends GenericHibernateDao<PrmsUser, String> {

	private UserDao(){}
	
	private static UserDao dao;
	
	public synchronized static UserDao getInstance(){
		if(dao == null )
			dao = new UserDao();
		return dao;
	}
	
	
	/**
	 * 用户列表
	 * huhui 2011-07-30
	 * @param bean  用户实体bean
	 * @param start 数据起始值
	 * @param limit 查询数量
	 * @return Page
	 */
	
    public Page getList(Session session,PrmsUser bean,Integer start, Integer limit){
    	System.out.println(" =========本地方法==============");
    	Page page = new Page();
        page.setStart(start);
        page.setLimit(limit);
        StringBuffer sb = new StringBuffer();
    	StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		if (Tools.isValid(bean.getCuserName())) {
			condition.append(" and obj.cuserName like ?");
			params.add("%"+bean.getCuserName() + "%");
		}
		if (Tools.isValid(bean.getCuserTruename())) {
			condition.append(" and obj.cuserTruename like ?");
			params.add("%"+bean.getCuserTruename()+ "%");
		}
		if (Tools.isValid(bean.getCuserMobile())) {
			condition.append(" and obj.cuserMobile like ?");
			params.add("%"+bean.getCuserMobile()+ "%");
		}
		if (Tools.isValid(bean.getChildListId())) {
//			StringUtil
			condition.append(" and obj.cuserId in (select cuserId from PrmsUserOrg where corgId in ("+bean.getChildListId()+"))");
			//params.add(bean.getChildListId());
		}
        sb.append(" from PrmsUser as obj where 1=1");
        sb.append(condition);
        Query query = session.createQuery(sb.toString());
        int i = 0;
		for (Object obj : params) {
			query.setParameter(i, obj);
			i++;
		}
        query.setFirstResult(start).setMaxResults(limit);
        
		Query totalQuery = session
		.createQuery("select count(*) from PrmsUser obj where 1=1 "+ condition);
		int j = 0;
		for (Object obj : params) {
			totalQuery.setParameter(j, obj);
			j++;
		}
        
        page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
        List list = new ArrayList();
        List<PrmsUser> voList = query.list();
        for(PrmsUser u:voList){
        	List orgNameList = session.createQuery("select puo.orgName from PrmsUserOrg as puo,PrmsUser obj where puo.cuserId = obj.cuserId and obj.cuserId='"+u.getCuserId()+"'").list();
     	   u.setOrgName(StringUtil.getNewStrByList2(orgNameList));
     	   list.add(u);
        }
		
        page.setRows(list);
        return page;
    }
    
    /**
     * 判断企业下是否有用户
     * @param session
     * @param orgId
     * @return
     */
    public long getCountByOrg(Session session,String userIds){
    	if(!StringUtil.isNull(userIds)){
    		String hql = "select count(*) from PrmsUser where cuserId in("+userIds+")"; 
        	Query countQuery = session.createQuery(hql);
        	return (long) ((Long) countQuery.uniqueResult()).intValue();
    	}
    	return 0;
    }
    
    
    
    public PrmsUser findUserbyName(Session session,String userName){
    	String hql =" from PrmsUser where cuserName='"+userName+"'";
    	Query query=session.createQuery(hql);
    	List list=query.list();
    	PrmsUser pu;
    	if(list!=null&&list.size()>0){
    		pu = (PrmsUser)list.get(0);
    	}else{
    		pu = null;
    	}
    	return pu;
    }
    /**
     * 根据组织查找用户
     * @param session
     * @param orgId
     * @return
     */
    public List<PrmsUser> findUserByOrgId(Session session,String orgId){
    	String hql = "from PrmsUserOrg where corgId ='"+orgId+"'";
    	Query query = session.createQuery(hql);
    	List<PrmsUserOrg> userOrglist = query.list();
    	List<PrmsUser> userlist= new ArrayList<PrmsUser>();
    	for (PrmsUserOrg prmsUserOrg : userOrglist) {
    		String sql = "from PrmsUser as obj where cuserId='"+prmsUserOrg.getCuserId()+"'";
    		PrmsUser po = this.findUniqueObjectBySQL(session, sql);
    		userlist.add(po);
		}
    	return userlist;
    }
}
